package com.example.netty.demo2;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class FutureExample {
//    第二种技术是使用Futures。Futures是一个抽象的概念，
//    它表示一个值，该值可能在某一点变得可用。
//    一个Future要么获得计算完的结果，要么获得计算失败后的异常。
//    Java在java.util.concurrent包中附带了Future接口，它使用Executor异步执行。
//    例如下面的代码，每传递一个Runnable对象到ExecutorService.submit()方法就会得到一个回调的Future，你能使用它检测是否执行完成。

    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newCachedThreadPool();
        Runnable task1 = new Runnable() {
            @Override
            public void run() {
                //do something
                System.out.println("i am task1.....");
            }
        };
        Callable<Integer> task2 = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                //do something
                System.out.println("i am task2.....");
                return new Integer(100);
            }
        };
        Future<?> f1 = executor.submit(task1);
        Future<Integer> f2 = executor.submit(task2);
        System.out.println("task1 is completed? " + f1.isDone());
        System.out.println("task2 is completed? " + f2.isDone());
        //waiting task1 completed
        while (true) {
            if(f1.isDone()){
                System.out.println("task1 completed.");
                break;
            }else{
                System.out.println("wait task1");
                Thread.sleep(1000);
            }
        }
        //waiting task2 completed
        while (true) {
            if(f2.isDone()){
                System.out.println("return value by task2: " + f2.get());
                break;
            }else{
                System.out.println("wait task2");
                Thread.sleep(1000);
            }
        }
    }

}
